package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.GuessActivity;
import flc.ast.adapter.GuessAdapter;
import flc.ast.bean.GuessBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.SelTimeDialog;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private GuessAdapter meetAdapter;
    private GuessAdapter hotAdapter;
    private GuessAdapter recreationAdapter;
    private GuessAdapter encyclopediaAdapter;
    private GuessAdapter masterAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        //聚会
        mDataBinding.rvMeetList.setLayoutManager(new GridLayoutManager(mContext, 2));
        meetAdapter = new GuessAdapter();
        mDataBinding.rvMeetList.setAdapter(meetAdapter);
        meetAdapter.setOnItemClickListener(this);

        //热门
        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 2));
        hotAdapter = new GuessAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);

        //娱乐
        mDataBinding.rvRecreationList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recreationAdapter = new GuessAdapter();
        mDataBinding.rvRecreationList.setAdapter(recreationAdapter);
        recreationAdapter.setOnItemClickListener(this);

        //知识百科
        mDataBinding.rvEncyclopediaList.setLayoutManager(new GridLayoutManager(mContext, 2));
        encyclopediaAdapter = new GuessAdapter();
        mDataBinding.rvEncyclopediaList.setAdapter(encyclopediaAdapter);
        encyclopediaAdapter.setOnItemClickListener(this);

        //达人
        mDataBinding.rvMasterList.setLayoutManager(new GridLayoutManager(mContext, 2));
        masterAdapter = new GuessAdapter();
        mDataBinding.rvMasterList.setAdapter(masterAdapter);
        masterAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        GuessBean bean = (GuessBean) adapter.getItem(position);
        if (adapter == meetAdapter) {
            selTimeDialog(position, bean);
        } else if (adapter == hotAdapter) {
            selTimeDialog(position + 5, bean);
        } else if (adapter == recreationAdapter) {
            selTimeDialog(position + 11, bean);
        } else if (adapter == encyclopediaAdapter) {
            selTimeDialog(position + 14, bean);
        } else if (adapter == masterAdapter) {
            selTimeDialog(position + 18, bean);
        }
    }

    @Override
    protected void initData() {
        //聚会
        List<GuessBean> listMeet = new ArrayList<>();
        String[] meetName = getContext().getResources().getStringArray(R.array.meet_type);
        String[] meetDetail = getContext().getResources().getStringArray(R.array.meet_detail);
        listMeet.add(new GuessBean(meetName[0], meetDetail[0], R.drawable.icon_jh_qzsk));
        listMeet.add(new GuessBean(meetName[1], meetDetail[1], R.drawable.icon_jh_wsxj));
        listMeet.add(new GuessBean(meetName[2], meetDetail[2], R.drawable.icon_jh_smql));
        listMeet.add(new GuessBean(meetName[3], meetDetail[3], R.drawable.icon_jh_bljl));
        listMeet.add(new GuessBean(meetName[4], meetDetail[4], R.drawable.icon_jh_xyrc));
        meetAdapter.setList(listMeet);
        //热门
        List<GuessBean> listHot = new ArrayList<>();
        String[] hotName = getContext().getResources().getStringArray(R.array.hot_type);
        String[] hotDetail = getContext().getResources().getStringArray(R.array.hot_detail);
        listHot.add(new GuessBean(hotName[0], hotDetail[0], R.drawable.icon_rm_sjtk));
        listHot.add(new GuessBean(hotName[1], hotDetail[1], R.drawable.icon_rm_wjc));
        listHot.add(new GuessBean(hotName[2], hotDetail[2], R.drawable.icon_rm_kgzw));
        listHot.add(new GuessBean(hotName[3], hotDetail[3], R.drawable.icon_rm_dyzzr));
        listHot.add(new GuessBean(hotName[4], hotDetail[4], R.drawable.icon_rm_wsch));
        listHot.add(new GuessBean(hotName[5], hotDetail[5], R.drawable.icon_rm_wdck));
        hotAdapter.setList(listHot);
        //娱乐
        List<GuessBean> listRecreation = new ArrayList<>();
        String[] recreationName = getContext().getResources().getStringArray(R.array.recreation_type);
        String[] recreationDetail = getContext().getResources().getStringArray(R.array.recreation_detail);
        listRecreation.add(new GuessBean(recreationName[0], recreationDetail[0], R.drawable.icon_yl_dmx));
        listRecreation.add(new GuessBean(recreationName[1], recreationDetail[1], R.drawable.icon_yl_ecy));
        listRecreation.add(new GuessBean(recreationName[2], recreationDetail[2], R.drawable.icon_yl_tym));
        recreationAdapter.setList(listRecreation);
        //知识百科
        List<GuessBean> listEncyclopedia = new ArrayList<>();
        String[] encyclopediaName = getContext().getResources().getStringArray(R.array.encyclopedia_type);
        String[] encyclopediaDetail = getContext().getResources().getStringArray(R.array.encyclopedia_detail);
        listEncyclopedia.add(new GuessBean(encyclopediaName[0], encyclopediaDetail[0], R.drawable.icon_zsbk_cyc));
        listEncyclopedia.add(new GuessBean(encyclopediaName[1], encyclopediaDetail[1], R.drawable.icon_zsbk_wsxb));
        listEncyclopedia.add(new GuessBean(encyclopediaName[2], encyclopediaDetail[2], R.drawable.icon_zsbk_dwsj));
        listEncyclopedia.add(new GuessBean(encyclopediaName[3], encyclopediaDetail[3], R.drawable.icon_zsbk_lsnxs));
        encyclopediaAdapter.setList(listEncyclopedia);
        //达人
        List<GuessBean> listMaster = new ArrayList<>();
        String[] masterName = getContext().getResources().getStringArray(R.array.master_type);
        String[] masterDetail = getContext().getResources().getStringArray(R.array.master_detail);
        listMaster.add(new GuessBean(masterName[0], masterDetail[0], R.drawable.icon_dr_ppk));
        listMaster.add(new GuessBean(masterName[1], masterDetail[1], R.drawable.icon_dr_zjm));
        listMaster.add(new GuessBean(masterName[2], masterDetail[2], R.drawable.icon_dr_wlws));
        listMaster.add(new GuessBean(masterName[3], masterDetail[3], R.drawable.icon_dr_cjyx));
        masterAdapter.setList(listMaster);
    }

    private void selTimeDialog(int position, GuessBean guessBean) {
        SelTimeDialog selTimeDialog = new SelTimeDialog(mContext, guessBean);
        selTimeDialog.setListener(new SelTimeDialog.IListener() {
            @Override
            public void onClickConfirm(int time) {
                GuessActivity.type = position;
                GuessActivity.time = time;
                startActivity(GuessActivity.class);
            }
        });
        selTimeDialog.show();
    }

}